/*
 * @Author: silence
 * @Date: 2023-03-15 09:17:19
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-01-04 11:04:15
 * @Description: 我的页面
 */
import 'package:ems_neutral_app/common/widgets/cubitWidget/SettingItem.dart';
import 'package:ems_neutral_app/config/AppInfo.dart';
import 'package:ems_neutral_app/pages/Home/Controller/AppManagerController.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/pages/Mine/MinePage/View/MinePageHeader.dart';
import 'package:ems_neutral_app/pages/Mine/Serve/ServePage.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../base/SilScaffold.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../bloc/userbloc/user_bloc.dart';
import '../../../main.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../../utils/eventBus/eventBus.dart';
import '../../../utils/router/ARouter.dart';
import '../../../utils/string/StringConstant.dart';
import '../../DeviceNetworking/DNCheckDevicePage.dart';
import '../AboutUs/AboutUsPage.dart';
import '../Business/BusinessPage.dart';
import '../Feedback/FeedbackPage.dart';
import '../Language/LanguageController.dart';
import '../Language/LanguageSetPage.dart';

class MinePage extends StatefulWidget {
  const MinePage({Key? key}) : super(key: key);

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {
  AppManagerController _appManagerController = AppManagerController();
  LanguageController _languageController =
      Get.put<LanguageController>(LanguageController());

  @override
  void initState() {
    super.initState();

    if (mounted) {
      bus.on(StringConstant.APPGotoTabbar, (page) {
        if (page == 2) {
          getCacheData();
        }
      });

      _appManagerController.getAppVersion();
    }
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      navBarHide: true,
      sideMargin: 0,
      backgroundColor: Colors.black,
      body: mainWidget(),
    );
  }

  // 获取缓存数据
  getCacheData() {
    // CacheUtils.cacheTotalStr().then((value) => setState(() {
    //       _cacheStr = value;
    //     }));
  }

  mainWidget() {
    List<Widget> slivers = [];
    // 顶部header
    slivers.add(SliverToBoxAdapter(child: MinePageHeader()));

    // 商务合作
    slivers.add(SliverToBoxAdapter(
      child: SettingItem(
        title: LangOf(context)?.mine?.business,
        leftImgPath: 'imgs/personal/personal_swhz.png',
        onTapFunc: () => ARoute.gotoPage(context, BusinessPage()),
      ),
    ));

    // 意见反馈
    slivers.add(SliverToBoxAdapter(
      child: SettingItem(
          title: LangOf(context)?.mine?.feeback,
          leftImgPath: 'imgs/personal/personal_fankui.png',
          onTapFunc: () => ARoute.gotoPage(context, FeedbackPage())),
    ));

    // 语言切换
    slivers.add(SliverToBoxAdapter(
      child: Obx(() {
        String userSettingLanguage =
            _languageController.userSettingLanguage.value;
        return SettingItem(
          title: LangOf(context)?.mine?.language,
          leftImgPath: 'imgs/personal/personal_yyqh.png',
          subTitle: _languageController.getCurLanguageStr(
              userSettingLanguage: userSettingLanguage),
          onTapFunc: () => ARoute.gotoPage(context, LanguageSetPage()),
        );
      }),
    ));

    // 关于我们
    slivers.add(SliverToBoxAdapter(
      child: SettingItem(
        title: LangOf(context)?.mine?.about_us,
        leftImgPath: 'imgs/personal/personal_aboutUs.png',
        onTapFunc: () => ARoute.gotoPage(context, AboutUsPage()),
      ),
    ));

    // 我的服务
    slivers.add(SliverToBoxAdapter(
      child: SettingItem(
          title: LangOf(context)?.bottom?.service,
          leftImgPath: 'imgs/personal/personal_concact_us.png',
          onTapFunc: () => ARoute.gotoPage(context, ServePage())),
    ));

    // 配网
    slivers.add(SliverToBoxAdapter(
      child: SettingItem(
          title: LangOf(context)?.deviceNetworking?.deviceNetworking, //'设备配网',
          leftImgPath: 'imgs/personal/personal_pei_wang.png',
          onTapFunc: () => Navigator.push(
                context,
                DNCheckDevicePage.route(),
              )),
    ));

    // 间隔
    slivers.add(SliverToBoxAdapter(
      child: Container(height: 10.w),
    ));

    /// 退出登录
    slivers.add(SliverToBoxAdapter(
      child: SilButton(
        title: LangOf(context)?.mine?.logout,
        titleColor: ColorUtils.color_theme,
        fontSize: 18.sp,
        margin: EdgeInsets.only(top: 20.w, bottom: 40.w),
        onTapFunc: signOutPrompt,
      ),
    ));

    return Container(
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(AppInfo.stationType == '3'
                  ? 'imgs/personal/personal_bg_e3.png'
                  : 'imgs/personal/personal_bg.png'),
              fit: BoxFit.fitWidth,
              alignment: Alignment.topCenter)),
      child: CustomScrollView(slivers: slivers),
    );
  }

  /// 退出登录确认提示
  signOutPrompt() {
    SilAlertDialog.showAlertDialog(
        context,
        SilAlertDialogData(
          title: LangOf(context)?.mine?.logout, //'温馨提示',
          cancelButtonTitle: LangOf(context)?.common?.cancel, //'取消',
          confrimButtonTitle: LangOf(context)?.common?.confirm, //'确认',
          content: LangOf(context)?.mine?.logoutDesc, //'是否退出登录'
        ), confrimFunc: () async {
      UserBlocTool.loginOut(navigatorKey.currentState!.context);
      if (Navigator.canPop(context)) Navigator.pop(context);
    });
  }
}
